package cn.kgc83.shengyin.controller;

import cn.kgc83.shengyin.vo.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import sun.awt.geom.AreaOp;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.*;
import java.util.UUID;

/**
 * @author oldliu
 * @since 1.0
 */
@RestController
public class CommonController {
    @Value("${upload-path}")
    private String path;

    @PostMapping("upload")
    public Map<String,Object> upload(@RequestParam(name="edit") MultipartFile file){
        Map<String,Object> m=new HashMap<>();
        System.out.println("????>>>??????"+file);
        if (file!=null &&!file.isEmpty()) {
            File f = new File(path);
            if (!f.exists())
                f.mkdirs();
            //原来的文件名
            String filename = file.getOriginalFilename();
            String houzhui = filename.substring(filename.lastIndexOf('.'));
            String newName = UUID.randomUUID() + houzhui;

            try {
                file.transferTo(new File(f, newName));
                m.put("code",0);
                List<Map<String,Object>> list=new ArrayList<>();
                list.add(new HashMap(){
                    {
                        this.put("id",newName);
                    }
                });
               m.put("data",list);
               // m.put("data","[{\"id\":\""+newName+"\"}]");
                //m.put("location",newName);
                return m;
                //return Result.success();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        m.put("code",415);
        m.put("msg","图片没有选择");
        return m;
       //
        // return Result.error("图片没有选择");
    }
}
